package com.ts.audiocamera

import android.Manifest
import android.content.pm.PackageManager
import android.media.AudioFormat
import android.media.AudioManager
import android.media.AudioTrack
import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.Surface
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.ts.utils.splayer
import java.io.File

class FFmepgActivity3 : AppCompatActivity() {
    var surface: Surface? = null
    var msplayer: splayer?=null
    var mSurfaceView:SurfaceView?= null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        Handler(Looper.getMainLooper()).post {
            while (true) {
                try {
                    Looper.loop()
                } catch (_: Throwable) {
                }
            }
        }


        setContentView(R.layout.activity_ffmepg3)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA ) != PackageManager.PERMISSION_GRANTED) {
            // 权限未授予，请求权限
            ActivityCompat.requestPermissions(this,
                arrayOf(
                    Manifest.permission.CAMERA, Manifest . permission . WRITE_EXTERNAL_STORAGE, Manifest . permission . MANAGE_EXTERNAL_STORAGE,
                    Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_EXTERNAL_STORAGE), 12);
        } else {
            // 权限已授予，打开摄像头
//            surfaceView=findViewById(R.id.camera_view) as SurfaceView
//            surfaceView?.holder?.addCallback(this)
        }
        mSurfaceView= findViewById(R.id.surface_view_ff)

        mSurfaceView?.holder?.addCallback(object :SurfaceHolder.Callback{
            override fun surfaceCreated(holder: SurfaceHolder) {
                surface=holder.surface;
            }

            override fun surfaceChanged(
                holder: SurfaceHolder,
                format: Int,
                width: Int,
                height: Int
            ) {

            }

            override fun surfaceDestroyed(holder: SurfaceHolder) {

            }
        })
        msplayer=mSurfaceView?.let { splayer(it) }
        findViewById<Button>(R.id.btn_button).setOnClickListener {
            play()
        }
    }

    private  val TAG = "FFmepgActivity3"
    fun play(){

        Log.e(TAG,Environment.getExternalStorageDirectory().toString()+"--")

        var folderurl = File(Environment.getExternalStorageDirectory(), "learnffmpeg_output.mp4").getAbsolutePath()


//      folderurl = "http://39.134.65.162/PLTV/88888888/224/3221225611/index.m3u8";
//        folderurl = "http://zhibo.hkstv.tv/livestream/mutfysrq/playlist.m3u8";
        msplayer = mSurfaceView?.let { splayer(it) }
        msplayer?.play(folderurl, surface)
    }


}